// 引入api
import {reqCartlist,reqCartadd,reqCartdelete} from "../../request/api"
import {createSelector} from "reselect"
// 初始值
const initState = {
    // 初始化data
    // 购物车列表
    cartlist: [],
    // 购物车添加
    cartadd: [],
    // 购物车删除
    cartdelete: [],
    // 全选
    tag:false,
}

// actionsTypes
const types = {
    // 修改数据 type
    // 购物车列表
    CHANGE_CARTLIST: "SHOP_CHANGE_CARTLIST",
    // 购物车添加
    CHANGE_CARTADD: "SHOP_CHANGE_CARTADD",
    // 购物车删除
    CHANGE_CARTDELETE: "SHOP_CHANGE_CARTDELETE",
    // 全选
    CHANGE_TAG: "SHOP_CHANGE_TAG",
    // 单选
    CHANGE_ONETAG: "SHOP_CHANGE_ONETAG",
}

// reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        // 购物车列表
        case types.CHANGE_CARTLIST:
            return {
                ...state,
                cartlist:action.cartlist
            }
        // 购物车添加
        case types.CHANGE_CARTADD:
            return {
                ...state,
                cartadd:action.cartadd
            }
        // 购物车删除
        case types.CHANGE_CARTDELETE:
            return {
                ...state,
                cartdelete:action.cartdelete
            }
        // 全选
        case types.CHANGE_TAG:
            state.cartlist[1].content.forEach(item=>{
                item.tag = action.tag
            })
            return {
                ...state,
                tag:action.tag
            }
        // 单选
        case types.CHANGE_ONETAG:
            state.cartlist[1].content[action.index].tag=action.tag
            state.tag = state.cartlist[1].content.every(item=>item.tag)
            return {
                ...state,
                cartlist:[...state.cartlist]
            }
        default:
            return state;
    }

}

// actionCreators组件派发的动作
export const actions={
    // 修改user的action
    // 购物车列表
    changeCartlist:(cartlist)=>({type:types.CHANGE_CARTLIST,cartlist}),
    // 购物车添加
    changeCartadd:(cartadd)=>({type:types.CHANGE_CARTADD,cartadd}),
    // 购物车删除
    changeCartdelete:(cartdelete)=>({type:types.CHANGE_CARTDELETE,cartdelete}),
    // 全选
    allcheck:(tag)=>({type:types.CHANGE_TAG,tag}),
    // 单选
    check:(index,tag)=>({type:types.CHANGE_ONETAG,index,tag}),
    
    // 请求(异步操作)
    // 购物车列表
    reqCartlist:()=>(dispatch,getState)=>{
        // 注意传参
        reqCartlist({uid:JSON.parse(sessionStorage.getItem("user")).uid}).then(res => {
            if (res.data.code === 200) {
                res.data.list.forEach(item => {
                    if (item.content) {
                        item.content.forEach(i => {
                            i.tag = false
                            
                        })
                    }
                })
                dispatch(actions.changeCartlist(res.data.list))
                //跳转页面
                // history.push("/index/store")
            }
        })
    },
    // 购物车添加
    reqCartadd:(id)=>(dispatch,getState)=>{
        reqCartadd({uid:JSON.parse(sessionStorage.getItem("user")).uid,type:1,goodsid:id,num:1}).then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeCartadd(res.data.list))
                dispatch(actions.reqCartlist())
            }
        })
    },
    // 购物车删除
    reqCartdelete:(id)=>(dispatch,getState)=>{
        // 注意传参
        reqCartdelete({id:id}).then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeCartdelete(res.data.list))
                dispatch(actions.reqCartlist())
            }
        })
    },
}

//selectors导出数据 
// 购物车列表
export const getCartlist=state=>state.shop.cartlist;
// 购物车添加
export const getCartadd=state=>state.shop.cartadd;
// 购物车删除
export const getCartdelete=state=>state.shop.cartdelete;
// 全选
export const getTag=state=>state.shop.tag;
// 单选
export const getOnetag=state=>state.shop.onetag;
// 计算价格
export const getAllprice=createSelector(
    [getCartlist,getTag],
    (cartlist,tag)=>{
        return cartlist[1]?cartlist[1].content.reduce((allprice,item)=>{
            return item.tag?(allprice+item.price*item.num):allprice
        },0):0
    }
);
  
export default reducer